(define (interleave s1 s2)
  (if (null? s1)
      s2
      (cons-stream (stream-car s1) (interleave s2 (stream-cdr s1)))))
(define (pair-1 s1 s2)
  (interleave
   (stream-map (lambda (x) (list (stream-car s1) x)) s2)
   (pair-1 (stream-cdr s1) s2)))
(define (pair-2 s t)
  (cons-stream
   (list (stream-car s) (stream-car t))
   (interleave
    (stream-map (lambda (x) (list (stream-car s) x))
		(stream-cdr t))
    (pair-2 (stream-cdr s) (stream-cdr t)))))
(define (pair-3 s t)
  (cons-stream
   (list (stream-car s) (stream-car t))
   (interleave
    (stream-map (lambda (x) (list (stream-car s) x))
		(stream-cdr t))
    (pair-3 (stream-cdr s) t))))

(define (part-show pre-num)
  (define (<= a b) (or (< a b) (= a b)))
  (define (displayline x)
    (display x)
    (newline)
    x)
  (define (--part-show stream)
    (define (-part-show num)
      (if (<= num pre-num)
	  (begin
	    (displayline (stream-ref stream num))
	    (-part-show (+ 1 num)))
	  (displayline "end")))
    (-part-show 0))
  --part-show)
  
(define show-s (part-show 20))
(define ones (cons-stream 1 ones))
(define integrates (cons-stream 1 (stream-map +
					      ones
					      integrates)))
(define (triple s x y)
  (define (-triple s x y)
    (cons-stream (list (stream-car s) (stream-car x) (stream-car y))
			     (interleave (stream-map (lambda (g) (cons (stream-car s) g))
						     (pair-2 (stream-cdr x) (stream-cdr y)))
					 (-triple (stream-cdr s) (stream-cdr x) (stream-cdr y)))))
  (define (rt? l)
    (define (_rt? a b c)
      (define (square x) (* x x))
      (= (square c) (+ (square a) (square b))))
    (_rt? (car l) (car (cdr l)) (car (cdr (cdr l)))))
  (stream-filter rt? (-triple s x y)))
